$(function() {
    $("#topBtn").click(setTop);
    $("#promoteBtn").click(setPromote);
    $("#deleteBtn").click(setDelete);
});

function like(btn, entityType, entityId, entityUserId, postId) {
    $.post(
        CONTEXT_PATH + "/like",
        {"entityType":entityType, "entityId":entityId, "entityUserId":entityUserId, "postId":postId},
        function (data) {
            data = $.parseJSON(data);
            if (data.code === 0) {
                $(btn).children('b').text(data.likeStatus ? '取消赞' : '赞');
                $(btn).children('i').text(data.likeCount);
            } else {
                alert(data.msg);
            }
        }
    );
}

// 置顶帖子
function setTop() {
    $.post(
        CONTEXT_PATH + "/discuss/top",
        {"postId":$("#postId").val()},
        function (data) {
            data = $.parseJSON(data);
            if (data.code === 0) {
                $("#topBtn").text(data.topped ? "取消置顶" : "置顶");
            } else {
                alert(data.msg);
            }
        }
    );
}

// 加精帖子
function setPromote() {
    $.post(
        CONTEXT_PATH + "/discuss/promote",
        {"postId":$("#postId").val()},
        function (data) {
            data = $.parseJSON(data);
            if (data.code === 0) {
                $("#promoteBtn").text(data.promoted ? "取消加精" : "加精");
            } else {
                alert(data.msg);
            }
        }
    );
}

// 删除帖子
function setDelete() {
    $.post(
        CONTEXT_PATH + "/discuss/delete",
        {"postId":$("#postId").val()},
        function (data) {
            data = $.parseJSON(data);
            if (data.code === 0) {
                location.href = CONTEXT_PATH + "/index";
            } else {
                alert(data.msg);
            }
        }
    );
}